package com.hxh.wechat.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hxh.wechat.model.RegisterUser;
import com.hxh.wechat.model.UserInfo;

public class UserDbHelper extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "User.db";

    public UserDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    private static final String TABLE_NAME = "users";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_USERNAME = "username";
    private static final String COLUMN_PASSWORD = "password";
    private static final String COLUMN_CITY = "city";
    private static final String COLUMN_SCHOOL = "school";
    private static final String COLUMN_birthday = "birthday";
    private static final String COLUMN_sex = "sex";
    // 创建表的SQL语句
    private static final String CREATE_TABLE_QUERY =
            "CREATE TABLE " + TABLE_NAME + "(" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_USERNAME + " TEXT NOT NULL, " +
                    COLUMN_PASSWORD + " TEXT NOT NULL, " +
                    COLUMN_SCHOOL + " TEXT NOT NULL, " +
                    COLUMN_CITY + " TEXT NOT NULL, " +
                    COLUMN_birthday + " TEXT NOT NULL, " +
                    COLUMN_sex + " TEXT NOT NULL);";

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE_QUERY);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 升级逻辑，如果需要更新表结构等操作，在此处实现
    }

    public RegisterUser getUserInfoByUsername(String username) {
        // 查询数据的SQL语句
        String SELECT_QUERY = "SELECT * FROM " + TABLE_NAME +
                " WHERE " + COLUMN_USERNAME + "='" + username + "';";
        // 执行查询操作
        Cursor cursor = this.getReadableDatabase().rawQuery(SELECT_QUERY, null);

        // 如果有符合条件的数据，则取第一条数据作为结果返回
        if (cursor.moveToFirst()) {
            RegisterUser userInfo = new RegisterUser(cursor.getString(1),
                    cursor.getString(2),
                    cursor.getString(3),
                    cursor.getString(4),
                    cursor.getString(5),
                    cursor.getString(6));
            return userInfo;

        }
        // 没有符合条件的数据，返回null
        return null;
    }

    public void insert(RegisterUser userInfo) {
        // 插入数据的SQL语句
        String INSERT_QUERY = "INSERT INTO " + TABLE_NAME +
                "(" + COLUMN_USERNAME + ", " + COLUMN_PASSWORD + ", " + COLUMN_sex + ", " +
                COLUMN_birthday + ", " + COLUMN_SCHOOL + ", " + COLUMN_CITY + ") VALUES ('" + userInfo.getUsername() + "', '" + userInfo.getPassword() + "', '" + userInfo.getSex() + "', '" + userInfo.getBirthday() + "', '" + userInfo.getSchool() + "', '" + userInfo.getCity() + "');";
        // 执行插入操作
        this.getWritableDatabase().execSQL(INSERT_QUERY);
    }
}